package com.turing.post.tuxedo.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.ActivateCard;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.cardtransactions.util.CardTransBusiness;
import com.turing.post.transaction.util.MrchBusiness;
import com.turing.post.tuxedo.util.IQuery;
import com.turing.post.tuxedo.util.LocalService;
import com.turing.post.tuxedo.util.LocalServiceImpl;
import com.turing.post.tuxedo.util.OQuery;
import com.turing.post.tuxedo.util.PCPSService;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransResponse;
import com.turing.post.tuxedo.util.TuxedoApplicationException;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "PCPS.SVC9001")
@Remote( { LocalService.class, PCPSService.class })
@Local
public class Query extends LocalServiceImpl implements LocalService,
		PCPSService {

	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao activateCardDaoImpl;

	@EJB(mappedName = "cardtransactions.impl.CardTransBusinessImpl")
	private CardTransBusiness cardTransBusinessImpl;

	@EJB(mappedName = "transaction.impl.MrchBusinessImpl")
	private MrchBusiness mrchBusinessImpl;

	/**
	 * generate IQuery
	 * 
	 * @return IQuery
	 */
	public IQuery makeRequestObject() {
		return new IQuery();
	}

	/**
	 * Query 数据检查
	 * 
	 * @param req
	 * @param ack
	 */
	protected void preService(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {
	}

	/**
	 * Query service entry 预付卡查询的业务逻辑
	 * 
	 * @param req
	 * @param ack
	 */
	protected void Service(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {

		IQuery itr = (IQuery) req;
		OQuery otr = (OQuery) ack;

		PCPSUtil.Log("CardNo:" + itr.cardObj.cardNo);

		ActivateCard ac;

		// 验证卡信息的完整性
		itr.cardObj.cardNo = mrchBusinessImpl.getValidateCardNo(req.merchantNo,
				itr.cardObj.cardNo, itr.cardObj.track2, itr.cardObj.track3);

		// 先按卡号查询
		ac = activateCardDaoImpl.getActivateCardByCardNo(itr.cardObj.cardNo);
		// 如未查询到结果,则按卡顺序号查询
		if (ac == null)
			ac = activateCardDaoImpl
					.getActivateCardByCardSeqNo(itr.cardObj.cardNo);

		if (ac == null)
			throw new TuxedoApplicationException(TuxedoRespCode.RC_INVALID,
					"卡无效");

		// 执行查询的业务逻辑
		ac = cardTransBusinessImpl.getValidActivateCard(ac.getCardNo(),
				itr.passwd, null);

		// 设置返回值
		otr.cardNo = ac.getCardNo();
		otr.expired = ac.getExpired();
		otr.balance = ac.getBalance();
	}

	/**
	 * Query 服务入口
	 * 
	 * @param req
	 * @return TransResponse
	 */
	public TransResponse Service(TransRequest req)
			throws PCPSApplicationException {
		// 按普通方法执行服务
		return CommonService(req, new OQuery());
	}
}